
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>3.4 RegExp类型 · gitbook电子书</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-toggle-chapters/toggle.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="global&math.html" />
    
    
    <link rel="prev" href="date.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    前言
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.1" data-path="../使用/使用.html">
            
                <a href="../使用/使用.html">
            
                    
                    md语法 & gitbook使用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../面试题/面试题.html">
            
                <a href="../面试题/面试题.html">
            
                    
                    面试题
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../面试题/html.html">
            
                <a href="../面试题/html.html">
            
                    
                    HTML
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../面试题/css.html">
            
                <a href="../面试题/css.html">
            
                    
                    CSS
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../面试题/js.html">
            
                <a href="../面试题/js.html">
            
                    
                    JavaScript
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../面试题/vue.html">
            
                <a href="../面试题/vue.html">
            
                    
                    Vue
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../面试题/小程序.html">
            
                <a href="../面试题/小程序.html">
            
                    
                    小程序
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="../面试题/编程题.html">
            
                <a href="../面试题/编程题.html">
            
                    
                    编程题
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../html/">
            
                <a href="../html/">
            
                    
                    1. HTML
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../html/1.常用标签.html">
            
                <a href="../html/1.常用标签.html">
            
                    
                    1.1 常用标签
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../html/2.列表.html">
            
                <a href="../html/2.列表.html">
            
                    
                    1.2 列表
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../html/3.表单.html">
            
                <a href="../html/3.表单.html">
            
                    
                    1.3 表单属性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../html/4.音频&视频.html">
            
                <a href="../html/4.音频&视频.html">
            
                    
                    1.4 ifame & audio & video
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../css/">
            
                <a href="../css/">
            
                    
                    2. CSS
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../css/1.选择器.html">
            
                <a href="../css/1.选择器.html">
            
                    
                    2.1 选择器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../css/2.字体样式.html">
            
                <a href="../css/2.字体样式.html">
            
                    
                    2.2 字体样式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../css/3.背景.html">
            
                <a href="../css/3.背景.html">
            
                    
                    2.3 背景
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../css/4.盒子模型.html">
            
                <a href="../css/4.盒子模型.html">
            
                    
                    2.4 盒子模型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../css/5.浮动.html">
            
                <a href="../css/5.浮动.html">
            
                    
                    2.5 浮动
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="../css/6.定位和渐变.html">
            
                <a href="../css/6.定位和渐变.html">
            
                    
                    2.6 定位 & 渐变
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="../css/7.过渡&变形.html">
            
                <a href="../css/7.过渡&变形.html">
            
                    
                    2.7 过渡 & 变形
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.8" data-path="../css/8.动画.html">
            
                <a href="../css/8.动画.html">
            
                    
                    2.8 动画
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.9" data-path="../css/9.滤镜.html">
            
                <a href="../css/9.滤镜.html">
            
                    
                    2.9 滤镜
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.10" data-path="../css/10.布局.html">
            
                <a href="../css/10.布局.html">
            
                    
                    2.10 布局
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="./">
            
                <a href="./">
            
                    
                    3. JS
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="数据类型.html">
            
                <a href="数据类型.html">
            
                    
                    3.1 数据类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="语句.html">
            
                <a href="语句.html">
            
                    
                    操作符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="操作符.html">
            
                <a href="操作符.html">
            
                    
                    语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="函数.html">
            
                <a href="函数.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="基本类型和引用类型的值.html">
            
                <a href="基本类型和引用类型的值.html">
            
                    
                    基本类型和引用类型的值
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="执行环境及作用域.html">
            
                <a href="执行环境及作用域.html">
            
                    
                    执行环境及作用域
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.7" data-path="垃圾收集.html">
            
                <a href="垃圾收集.html">
            
                    
                    垃圾收集
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.8" data-path="Object类型.html">
            
                <a href="Object类型.html">
            
                    
                    Object类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.9" data-path="Array类型.html">
            
                <a href="Array类型.html">
            
                    
                    Array类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.10" data-path="Date类型.html">
            
                <a href="Date类型.html">
            
                    
                    Date类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.11" data-path="string&array.html">
            
                <a href="string&array.html">
            
                    
                    3.2 String & Array
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.12" data-path="date.html">
            
                <a href="date.html">
            
                    
                    3.3 Date类型
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.5.13" data-path="正则表达式.html">
            
                <a href="正则表达式.html">
            
                    
                    3.4 RegExp类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.14" data-path="global&math.html">
            
                <a href="global&math.html">
            
                    
                    3.5 Global对象 & Math对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.15" data-path="变量&作用域&垃圾收集.html">
            
                <a href="变量&作用域&垃圾收集.html">
            
                    
                    3.6 变量 & 作用域 & 垃圾收集
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.16" data-path="流程控制&函数.html">
            
                <a href="流程控制&函数.html">
            
                    
                    3.7 流程控制 & 函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.17" data-path="面向对象.html">
            
                <a href="面向对象.html">
            
                    
                    3.8 面向对象的程序设计
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.18" data-path="递归&冒泡&快速.html">
            
                <a href="递归&冒泡&快速.html">
            
                    
                    3.9 递归 & 冒泡 & 快速
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.19" data-path="定时器&防抖&节流.html">
            
                <a href="定时器&防抖&节流.html">
            
                    
                    3.10 定时器\防抖\节流
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.20" data-path="DOM获取&DOM操作.html">
            
                <a href="DOM获取&DOM操作.html">
            
                    
                    3.11 DOM获取 & DOM操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.21" data-path="键盘事件&鼠标事件.html">
            
                <a href="键盘事件&鼠标事件.html">
            
                    
                    3.12 键盘事件 & 鼠标事件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.22" data-path="事件冒泡&事件代理&事件绑定.html">
            
                <a href="事件冒泡&事件代理&事件绑定.html">
            
                    
                    3.13 事件冒泡\代理\绑定
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.23" data-path="this指向.html">
            
                <a href="this指向.html">
            
                    
                    3.14 this指向
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.24" data-path="Ajax&跨域.html">
            
                <a href="Ajax&跨域.html">
            
                    
                    3.15 Ajax & 跨域
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.25" data-path="Bom.html">
            
                <a href="Bom.html">
            
                    
                    3.16 BOM
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.26" data-path="ES6.html">
            
                <a href="ES6.html">
            
                    
                    3.17 ES6
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.27" data-path="js实例.html">
            
                <a href="js实例.html">
            
                    
                    3.18 js实例
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../vue/">
            
                <a href="../vue/">
            
                    
                    4. Vue
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../vue/1.基础.html">
            
                <a href="../vue/1.基础.html">
            
                    
                    4.1 基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../vue/2.生命周期.html">
            
                <a href="../vue/2.生命周期.html">
            
                    
                    4.2 生命周期
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../vue/3.vuex.html">
            
                <a href="../vue/3.vuex.html">
            
                    
                    4.3 vuex
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="../vue/4.原理.html">
            
                <a href="../vue/4.原理.html">
            
                    
                    4.4 深入响应式原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" data-path="../vue/5.组件.html">
            
                <a href="../vue/5.组件.html">
            
                    
                    4.5 组件进阶
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.6" data-path="../vue/6.规模化.html">
            
                <a href="../vue/6.规模化.html">
            
                    
                    4.6 规模化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.7" data-path="../vue/7.过渡&动画.html">
            
                <a href="../vue/7.过渡&动画.html">
            
                    
                    4.7 过渡 & 动画
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.8" data-path="../vue/8.可复用性&组合.html">
            
                <a href="../vue/8.可复用性&组合.html">
            
                    
                    4.8 可复用性 & 组合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.9" data-path="../vue/9.工具.html">
            
                <a href="../vue/9.工具.html">
            
                    
                    4.9 工具
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../Node/">
            
                <a href="../Node/">
            
                    
                    5. Node
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../插件/">
            
                <a href="../插件/">
            
                    
                    插件
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >3.4 RegExp类型</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;">&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;</h1>
<h3 id="&#x6B63;&#x5219;&#x5199;&#x6CD5;">&#x6B63;&#x5219;&#x5199;&#x6CD5;</h3>
<pre><code class="lang-js">    <span class="hljs-comment">//1.&#x76F4;&#x63A5;&#x5199;&#x8868;&#x8FBE;&#x5F0F;</span>
    <span class="hljs-keyword">var</span> reg1 = <span class="hljs-regexp">/abc/</span>;
    <span class="hljs-comment">//2.&#x521B;&#x5EFA;&#x5B9E;&#x5217;&#x5316;&#x5BF9;&#x8C61;</span>
    <span class="hljs-keyword">var</span> reg2 = <span class="hljs-keyword">new</span> <span class="hljs-built_in">RegExp</span>(<span class="hljs-string">`abc`</span>);
    <span class="hljs-keyword">var</span> reg2 = <span class="hljs-keyword">new</span> <span class="hljs-built_in">RegExp</span>(<span class="hljs-regexp">/abc/</span>);
</code></pre>
<h3 id="&#x6807;&#x5FD7;">&#x6807;&#x5FD7;</h3>
<pre><code class="lang-js">    <span class="hljs-comment">//g--&gt;&#x5168;&#x5C40;  </span>
    <span class="hljs-comment">//i--&gt;&#x5FFD;&#x7565;&#x5927;&#x5C0F;&#x5199;</span>
    <span class="hljs-comment">//m--&gt;&#x591A;&#x884C;  u--&gt;Unicode  y--&gt;&#x7C98;&#x6027;&#x5339;&#x914D;</span>
    <span class="hljs-keyword">var</span> reg1 = <span class="hljs-regexp">/abc/g</span>;
    <span class="hljs-keyword">var</span> reg2 = <span class="hljs-keyword">new</span> <span class="hljs-built_in">RegExp</span>(<span class="hljs-string">`abc`</span>,<span class="hljs-string">&quot;gi&quot;</span>);
</code></pre>
<h3 id="&#x6B63;&#x5219;&#x65B9;&#x6CD5;">&#x6B63;&#x5219;&#x65B9;&#x6CD5;</h3>
<h5 id="1-test">1. test()</h5>
<pre><code class="lang-js">    <span class="hljs-comment">//&#x5339;&#x914D;&#x5185;&#x5BB9;&#x662F;&#x5426;&#x5B58;&#x5728;&#x5B57;&#x7B26;&#x4E32;&#x4E2D;</span>
    <span class="hljs-comment">//&#x6B63;&#x5219;&#x5BF9;&#x8C61;.test(&#x5339;&#x914D;&#x5B57;&#x7B26;&#x4E32;)&#x8FD4;&#x56DE; true &#x6216;&#x8005;false</span>
    <span class="hljs-keyword">var</span> str1 = <span class="hljs-string">&quot;abcdABCD&quot;</span>;
    <span class="hljs-built_in">console</span>.log(reg1.test(str1))
</code></pre>
<h5 id="2-exec">2. exec()</h5>
<pre><code class="lang-js">    <span class="hljs-comment">//exec</span>
    <span class="hljs-comment">//&#x8FD4;&#x56DE;&#x503C;&#xFF1A;&#x5982;&#x679C;&#x5B58;&#x5728;&#xFF0C;&#x8FD4;&#x56DE;&#x6570;&#x7EC4;&#xFF1B;&#x5982;&#x679C;&#x4E0D;&#x5B58;&#x5728;&#xFF0C;&#x8FD4;&#x56DE;null</span>
    <span class="hljs-comment">//&#x8FD4;&#x56DE;&#x6570;&#x7EC4;&#xFF1A;&#x5305;&#x542B;&#x5339;&#x914D;&#x51FA;&#x6765;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x7247;&#x6BB5;&#x3001;index&#x8868;&#x793A;&#x5339;&#x914D;&#x7684;&#x7D22;&#x5F15;&#x4F4D;&#x7F6E;&#x3001;group&#x3001;input&#x8868;&#x793A;&#x539F;&#x672C;&#x7684;&#x5B57;&#x7B26;&#x4E32;</span>

    <span class="hljs-comment">//&#x6B63;&#x5219;&#x5BF9;&#x8C61;.lastIndex &#x8868;&#x793A;&#x5339;&#x914D;&#x540E;&#x7684;&#x66F4;&#x65B0;&#x7684; &#x4E0B;&#x4E00;&#x6B21;&#x5339;&#x914D;&#x7D22;&#x5F15;&#x8D77;&#x59CB;&#x4F4D;&#x7F6E;&#x3001;&#x5982;&#x679C;&#x5339;&#x914D;&#x5931;&#x8D25;&#xFF0C;lastIndex&#x5F52;&#x96F6;</span>
    <span class="hljs-built_in">console</span>.log(reg2.exec(str1),reg2.lastIndex);<span class="hljs-comment">//lastIndex 3</span>
    <span class="hljs-built_in">console</span>.log(reg2.exec(str1),reg2.lastIndex);<span class="hljs-comment">//lastIndex 7</span>
    <span class="hljs-built_in">console</span>.log(reg2.exec(str1),reg2.lastIndex);<span class="hljs-comment">//lastIndex 0</span>
</code></pre>
<h5 id="3-match--search">3. match() &amp; search</h5>
<pre><code class="lang-js">    <span class="hljs-comment">//&#x5B57;&#x7B26;&#x4E32;.match&#xFF08;&#x6B63;&#x5219;&#xFF09;</span>
    <span class="hljs-keyword">var</span> str3 = <span class="hljs-string">&quot;today is Monday&quot;</span>
    <span class="hljs-keyword">var</span> reg3 = <span class="hljs-regexp">/day/gi</span>;
    <span class="hljs-comment">//&#x5982;&#x679C;&#x6B63;&#x5219;&#x53EA;&#x5339;&#x914D;&#x4E0D;&#x5305;&#x542B;&#x6807;&#x5FD7;g&#xFF0C;&#x8FD4;&#x56DE;&#x7684;&#x5185;&#x5BB9;&#x548C;exec&#x7684;&#x6548;&#x679C;&#x4E00;&#x6837;&#xFF0C;&#x4E0D;&#x8FC7;&#x6BCF;&#x6B21;&#x8C03;&#x7528;&#x7684;&#x7ED3;&#x679C;&#x90FD;&#x662F;&#x4E00;&#x81F4;&#x7684;&#xFF0C;&#x6B63;&#x5219;&#x7684;lastIndex&#x4E5F;&#x4E0D;&#x4F1A;&#x66F4;&#x65B0;</span>
    <span class="hljs-comment">//&#x5982;&#x679C;&#x6B63;&#x5219;&#x5305;&#x542B;&#x6807;&#x5FD7;g&#xFF0C;&#x8FD4;&#x56DE;&#x7684;&#x662F;&#x5305;&#x542B;&#x5339;&#x914D;&#x7247;&#x6BB5;&#x7684;&#x6570;&#x7EC4;</span>
    <span class="hljs-built_in">console</span>.log(str3.match(reg3),reg3.lastIndex);
    <span class="hljs-built_in">console</span>.log(str3.match(reg3),reg3.lastIndex);

    <span class="hljs-comment">//&#x5B57;&#x7B26;&#x4E32;.search&#xFF08;&#x6B63;&#x5219;&#xFF09;</span>
    <span class="hljs-comment">//&#x8FD4;&#x56DE;&#x5339;&#x914D;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x4E0B;&#x6807;</span>
    <span class="hljs-built_in">console</span>.log(str3.search(<span class="hljs-regexp">/day/g</span>))

    <span class="hljs-comment">/*********&#x5339;&#x914D;&#x89C4;&#x5219;********/</span>
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;*********&#x5339;&#x914D;&#x89C4;&#x5219;********&quot;</span>)
    <span class="hljs-comment">//.&#x8868;&#x793A;&#x4EFB;&#x610F;&#x5B57;&#x7B26;</span>
    <span class="hljs-keyword">var</span> str4 = <span class="hljs-string">&quot;today is Monday,not a good day&quot;</span>;
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;.&quot;</span>,str4.match(<span class="hljs-regexp">/..y../g</span>))

    <span class="hljs-comment">// \d&#x8868;&#x793A;&#x6570;&#x5B57;&#xFF0C;&#x7B49;&#x4EF7;&#x4E8E;[0-9]   &#x4EC5;&#x4EE3;&#x8868;&#x4E00;&#x4E2A;&#x6570;&#x5B57;&#x4F4D;</span>
    <span class="hljs-keyword">var</span> str5 = <span class="hljs-string">&quot;123ABC&quot;</span>
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;\\d&quot;</span>,str5.match(<span class="hljs-regexp">/\d\d/</span>));
    <span class="hljs-comment">// \D&#x8868;&#x793A;&#x975E;&#x963F;&#x62C9;&#x4F2F;&#x6570;&#x5B57;&#xFF0C;&#x7B49;&#x4EF7;&#x4E8E;[^0-9]   &#x4EC5;&#x4EE3;&#x8868;&#x4E00;&#x4E2A;&#x6570;&#x5B57;&#x4F4D;</span>
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;\\D&quot;</span>,str5.match(<span class="hljs-regexp">/\D\D/</span>));

    <span class="hljs-comment">//\w &#x8868;&#x793A;[A-Za-z0-9_]&#x7684;&#x96C6;&#x5408;</span>
    <span class="hljs-keyword">var</span> str6 = <span class="hljs-string">&quot;123&#xFF01;@ABC&quot;</span>
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;\\w&quot;</span>,str6.match(<span class="hljs-regexp">/\w/</span>));

    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;\\W&quot;</span>,str6.match(<span class="hljs-regexp">/\W/</span>));

    <span class="hljs-comment">//\s &#x8868;&#x793A;&#x5339;&#x914D;&#x4E00;&#x4E2A;&#x7A7A;&#x767D;&#x5B57;&#x7B26;&#xFF0C;&#x5305;&#x62EC;&#x7A7A;&#x683C;&#x3001;&#x5236;&#x8868;&#x7B26;&#x3001;&#x6362;&#x9875;&#x7B26;&#x548C;&#x6362;&#x884C;&#x7B26;&#x3001;&#x5176;&#x4ED6;Unicode &#x7A7A;&#x683C;&#x3002;</span>
    <span class="hljs-keyword">var</span> str7 = <span class="hljs-string">&quot;hello world&quot;</span>
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;\\s&quot;</span>,str7.match(<span class="hljs-regexp">/\s/</span>));
    <span class="hljs-comment">//\S &#x8868;&#x793A;&#x5339;&#x914D;&#x4E00;&#x4E2A;&#x975E;&#x7A7A;&#x767D;&#x7B26;[^]</span>
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;\\S&quot;</span>,str7.match(<span class="hljs-regexp">/\S/</span>));

    <span class="hljs-keyword">var</span> str8 = <span class="hljs-string">&quot;control-M&quot;</span>;
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;\\cM&quot;</span>,str8.match(<span class="hljs-regexp">/\cM/</span>));

    <span class="hljs-comment">//\xhh hh&#x8868;&#x793A;&#x5341;&#x516D;&#x8FDB;&#x5236;&#x6570;&#x7684;ASCII&#x7F16;&#x53F7;</span>
    <span class="hljs-keyword">var</span> str9 = <span class="hljs-string">&quot;abcAabcB&quot;</span>;
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;\\x42&quot;</span>,str9.match(<span class="hljs-regexp">/\x42/</span>))

    <span class="hljs-comment">//\uhhhh hhhh&#x8868;&#x793A;Unicode&#x7F16;&#x7801;</span>
    <span class="hljs-keyword">var</span> str10 = <span class="hljs-string">&quot;&#x4ECA;&#x5929;&#x662F;&#x5468;&#x4E00;&quot;</span>;
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;\\u4E00&quot;</span>,str10.match(<span class="hljs-regexp">/[\u4E00-\u9FA5]/</span>))

    <span class="hljs-comment">//^XX &#x8868;&#x793A;&#x5355;&#x8BCD;&#x8FB9;&#x754C;&#xFF0C;&#x5339;&#x914D;&#x5B57;&#x7B26;&#x4E32;&#x5FC5;&#x987B;&#x4EE5;xx&#x5F00;&#x5934;&#xFF0C;</span>
    <span class="hljs-keyword">var</span> str11 = <span class="hljs-string">&quot;str11&quot;</span>;
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;\^&quot;</span>,str11.match(<span class="hljs-regexp">/^\w/</span>));<span class="hljs-comment">//js&#x53D8;&#x91CF;&#x547D;&#x540D;(^[A-Za-z_$])</span>
    <span class="hljs-comment">//xx$ &#x8868;&#x793A;&#x5FC5;&#x987B;&#x4EE5;xx&#x7ED3;&#x5C3E;</span>
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;\$&quot;</span>,str11.match(<span class="hljs-regexp">/\d$/</span>));

    <span class="hljs-comment">// \b &#x8868;&#x793A;&#x96F6;&#x5BBD;&#x5355;&#x8BCD;  &#x4F8B;&#x5982;&#x5355;&#x8BCD;&#x7684;&#x5F00;&#x5934;&#x6216;&#x7ED3;&#x675F;</span>
    <span class="hljs-keyword">var</span> str12 = <span class="hljs-string">&quot;hello world&quot;</span>;
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;\b&quot;</span>,str12.match(<span class="hljs-regexp">/ello\b/</span>));

    <span class="hljs-comment">//() &#x8868;&#x793A;&#x6355;&#x83B7;&#x5185;&#x5BB9;&#xFF0C;&#x53EF;&#x4EE5;&#x5728;&#x7ED3;&#x679C;&#x6570;&#x7EC4;&#x7684;&#x5143;&#x7D20;[1],&#x2026;&#x2026;&#xFF0C;[n]&#x4E2D;&#x627E;&#x5230;</span>
    <span class="hljs-keyword">var</span> str13 = <span class="hljs-string">&quot;str=13 str=14&quot;</span>
    <span class="hljs-comment">//[0]:str=13   [1]:13</span>
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;()&quot;</span>,str13.match(<span class="hljs-regexp">/str=(\d\d)/</span>));
    <span class="hljs-comment">//[0]:13       [1]:13</span>
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;()&quot;</span>,str13.match(<span class="hljs-regexp">/(\d\d)/</span>));

    <span class="hljs-comment">//\n&#x8868;&#x793A;&#x5F15;&#x7528;&#x524D;&#x9762;&#x7684;&#xFF08;&#xFF09;&#x5185;&#x7684;&#x5339;&#x914D;&#x89C4;&#x5219;&#xFF0C;n&#x4EE3;&#x8868;&#x524D;&#x9762;&#x7684;&#x7B2C;&#x51E0;&#x4E2A;&#xFF08;&#xFF09;</span>
    <span class="hljs-keyword">var</span> str14 = <span class="hljs-string">&quot;cherry!apple,orange,cherry,peach.&quot;</span>;
    <span class="hljs-comment">//&#xFF08;&#xFF01;&#xFF09;&#xFF1A;1&#x53F7;    &#xFF08;&#xFF0C;&#xFF09;&#xFF1A;2&#x53F7;</span>
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;\\n&quot;</span>,str14.match(<span class="hljs-regexp">/(!)apple(,)orange\2/</span>));

    <span class="hljs-keyword">var</span> str15 = <span class="hljs-string">&quot;str=13 str=&#x5341;&#x56DB;&quot;</span>
    <span class="hljs-comment">//&#x76F8;&#x5F53;&#x4E8E;&#x4E0D;</span>
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;(?:)&quot;</span>,str15.match(<span class="hljs-regexp">/str=(?:\d\d)/</span>));
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;()&quot;</span>,str15.match(<span class="hljs-regexp">/str=\d\d/</span>));


    <span class="hljs-comment">/*********&#x6570;&#x91CF;***********/</span>
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;*********&#x6570;&#x91CF;***********&quot;</span>);
    <span class="hljs-keyword">var</span> str16 = <span class="hljs-string">&quot;string123456&quot;</span>
    <span class="hljs-comment">//* &#x4EE3;&#x8868;0&#x5230;&#x591A;&#x6B21;&#x91CD;&#x590D;</span>
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;*&quot;</span>,str16.match(<span class="hljs-regexp">/^[A-Za-z_$]\w*/</span>))
    <span class="hljs-comment">//+ &#x4EE3;&#x8868;1&#x5230;&#x591A;&#x6B21;&#x91CD;&#x590D;</span>
    <span class="hljs-keyword">var</span> str17 = <span class="hljs-string">&quot;s1234567&quot;</span>
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;+&quot;</span>,str17.match(<span class="hljs-regexp">/[a-z]+/</span>))
    <span class="hljs-comment">//</span>
    <span class="hljs-keyword">var</span> str18 = <span class="hljs-string">&quot;xxx12345678&quot;</span>
    <span class="hljs-comment">//8&#x4E2A;{8}    &#x81F3;&#x5C11;8&#x4E2A;   {8&#xFF0C;}    &#x81F3;&#x5C11;8&#x4E2A;&#xFF0C;&#x6700;&#x591A;20&#x4E2A; {8,20}</span>
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;{n&#xFF0C;m}&quot;</span>,str18.match(<span class="hljs-regexp">/xxx\d{8}/</span>))

    <span class="hljs-comment">//x?  x&#x51FA;&#x73B0;  0&#x6216;1&#x6B21;</span>
    <span class="hljs-keyword">var</span> str19 = <span class="hljs-string">&quot;s111&quot;</span>
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;&#xFF1F;&quot;</span>,str19.match(<span class="hljs-regexp">/s\d?/</span>))
    <span class="hljs-comment">//*?  *&#x4EE3;&#x8868;&#x91CD;&#x590D;0&#x5230;&#x591A;&#x6B21;     *?&#x6700;&#x5C0F;&#x6B21;&#x6570;0</span>
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;*&#xFF1F;&quot;</span>,str19.match(<span class="hljs-regexp">/s\d*?/</span>))<span class="hljs-comment">//s</span>
    <span class="hljs-comment">//+&#xFF1F;  +&#x4EE3;&#x8868;&#x91CD;&#x590D;1&#x5230;&#x591A;&#x6B21;     +?&#x6700;&#x5C0F;&#x6B21;&#x6570;1</span>
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;+?&quot;</span>,str19.match(<span class="hljs-regexp">/s\d+?/</span>))


    <span class="hljs-keyword">var</span> str20 = <span class="hljs-string">&quot;Jack Sprat&quot;</span>;
    <span class="hljs-comment">//?= &#x76F8;&#x5F53;&#x4E8E;&#x5339;&#x914D;Jack&#xFF0C;&#x540E;&#x9762;&#x7684;Sprat&#x53EA;&#x662F;&#x6761;&#x4EF6;&#xFF0C;&#x5411;&#x524D;&#x65AD;&#x8A00;&#xFF08;&#x53EA;&#x80FD;&#x9488;&#x5BF9;&#x524D;&#x9762;&#xFF09;</span>
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;?=&quot;</span>,str20.match(<span class="hljs-regexp">/Jack (?=Sprat)/</span>))
    <span class="hljs-comment">//x(?!y)x&#x540E;&#x9762;&#x4E0D;&#x80FD;&#x7D27;&#x8DDF;y</span>
    <span class="hljs-keyword">var</span> str21 = <span class="hljs-string">&quot;3.141&quot;</span>;
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;?!&quot;</span>,str21.match(<span class="hljs-regexp">/\d(?!\.)/</span>))
    <span class="hljs-comment">//&#x6216;</span>
    <span class="hljs-keyword">var</span> str22 = <span class="hljs-string">&quot;Jack Sprat&quot;</span>;
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;x|y&quot;</span>,str22.match(<span class="hljs-regexp">/Jack (?=Sprat|haha)/</span>));
</code></pre>
<p>&#x7EC3;&#x4E60;&#xFF1A;JS&#x53D8;&#x91CF;&#x547D;&#x540D;&#x89C4;&#x5219;</p>
<pre><code class="lang-js">    <span class="hljs-comment">//\w&#x548C;$,&#x4F46;&#x4E0D;&#x5141;&#x8BB8;&#x6570;&#x5B57;&#x5F00;&#x5934;</span>
    <span class="hljs-comment">//(^[A-Za-z_$])</span>
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">test_var</span>(<span class="hljs-params"></span>)</span>{
        <span class="hljs-keyword">var</span> inp = <span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">&quot;#test_var&quot;</span>);
        <span class="hljs-comment">//&#x83B7;&#x53D6;&#x8F93;&#x5165;&#x6846;&#x7684;&#x503C;</span>
        <span class="hljs-keyword">var</span> value = <span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">&quot;#test_var&quot;</span>).value;
        <span class="hljs-built_in">console</span>.log(<span class="hljs-regexp">/^[A-Za-z_$][\w$]*$/</span>.exec(value))
        <span class="hljs-keyword">if</span>(<span class="hljs-regexp">/^[A-Za-z_$][\w$]*$/</span>.test(value)){
            inp.style.borderColor = <span class="hljs-string">&quot;green&quot;</span>;
            inp.style.outlineColor = <span class="hljs-string">&quot;green&quot;</span>;
        }<span class="hljs-keyword">else</span>{
            inp.style.borderColor = <span class="hljs-string">&quot;red&quot;</span>;
            inp.style.outlineColor = <span class="hljs-string">&quot;red&quot;</span>;
        }
    }

    <span class="hljs-comment">//&#x8F93;&#x5165;&#x8D26;&#x6237;&#x540D; &#x8981;&#x6C42; 4&#x5230;12&#x4E2A;&#x5B57;&#x6BCD;&#x6216;&#x6570;&#x5B57;&#x7EC4;&#x5408;</span>
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">check_name</span>(<span class="hljs-params">el</span>)</span>{
        <span class="hljs-keyword">var</span> value = el.value;
        <span class="hljs-built_in">console</span>.log(<span class="hljs-regexp">/[A-Za-z0-9]/</span>{<span class="hljs-number">4</span>,<span class="hljs-number">12</span>}.exec(value));
        <span class="hljs-keyword">if</span>(){
            el.style.borderColor = <span class="hljs-string">&quot;green&quot;</span>
        }<span class="hljs-keyword">else</span>{
            el.style.borderColor = <span class="hljs-string">&quot;red&quot;</span>
        }
    }
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="date.html" class="navigation navigation-prev " aria-label="Previous page: 3.3 Date类型">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="global&math.html" class="navigation navigation-next " aria-label="Next page: 3.5 Global对象 & Math对象">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"3.4 RegExp类型","level":"1.5.13","depth":2,"next":{"title":"3.5 Global对象 & Math对象","level":"1.5.14","depth":2,"path":"JS/global&math.md","ref":"JS/global&math.md","articles":[]},"previous":{"title":"3.3 Date类型","level":"1.5.12","depth":2,"path":"JS/date.md","ref":"JS/date.md","articles":[]},"dir":"ltr"},"config":{"plugins":["toggle-chapters","livereload"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"toggle-chapters":{},"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"gitbook电子书","gitbook":"*","description":"记录前端相关的知识点以及一些示例的demo"},"file":{"path":"JS/正则表达式.md","mtime":"2019-03-22T03:10:06.458Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-09-06T08:37:43.293Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-toggle-chapters/toggle.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

